package com.buer.storage.api;


import com.buer.storage.business.domain.Area;
import com.buer.storage.business.domain.Warehouse;
import com.buer.storage.business.dto.DepartmentDto;
import com.buer.storage.business.dto.DepartmentSelectDto;
import com.buer.storage.business.dto.WarehouseDto;
import com.buer.storage.business.service.WarehouseService;
import com.buer.storage.common.annotation.Auth;
import com.buer.storage.common.eumn.ResultEnum;
import com.buer.storage.common.utils.BaseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {
    @Resource
    private WarehouseService warehouseService;

    //增加仓库
    @Auth
    @PostMapping("/add.do")
    public BaseResult add(@RequestBody WarehouseDto warehouseDto){
        if(warehouseService.insert(warehouseDto)>0){
            //增加仓库成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //添加仓库失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //删除仓库
    @Auth
    @GetMapping("/delete.do")
    public BaseResult delete(int warehouseId){
        if(warehouseService.delete(warehouseId)>0) {
            //删除仓库成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //删除仓库失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //修改仓库
    @Auth
    @PostMapping("/update.do")
    public BaseResult update(@RequestBody Warehouse warehouse) {
        if (warehouseService.update(warehouse) > 0) {
            //修改仓库成功
            return BaseResult.build(ResultEnum.SUCCESS);
        }
        //修改仓库失败
        return BaseResult.build(ResultEnum.FAILURE);
    }

    //根据id查询仓库
    @Auth
    @GetMapping("/selectById.do")
    public BaseResult selectById(int warehouseId){
        Warehouse warehouse = warehouseService.selectById(warehouseId);
        return BaseResult.build(ResultEnum.SUCCESS,warehouse);
    }

    //查询所有仓库
    @Auth
    @GetMapping("/findAll.do")
    public BaseResult findAll(){
        List<Warehouse> all = warehouseService.findAll();
        return BaseResult.build(ResultEnum.SUCCESS,all);
    }

    //根据仓库id查询库区信息
    @Auth
    @GetMapping("/findAllArea.do")
    public BaseResult findAllArea(int warehouseId){
        List<Area> list = warehouseService.findAllArea(warehouseId);
        return BaseResult.build(ResultEnum.SUCCESS,list);
    }

    //根据仓库id查询库位信息
    @Auth
    @GetMapping("/findAllDepartment.do")
    public BaseResult findAllDepartment(int warehouseId){
        List<DepartmentDto> department = warehouseService.findAllDepartment(warehouseId);
        return BaseResult.build(ResultEnum.SUCCESS,department);
    }

    @Auth
    @GetMapping("/findDepartById.do")
    public BaseResult findDepartById(Integer warehouseId){
        List<DepartmentSelectDto> departById = warehouseService.findDepartById(warehouseId);
        return BaseResult.build(ResultEnum.SUCCESS,departById);
    }
}
